package com.dhcc.DRGsHosp.application.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DRGVersionApplication;
import com.dhcc.DRGsHosp.core.domain.DRGVersion;

@Named
@Transactional
public class DRGVersionApplicationImpl implements DRGVersionApplication,Serializable {
	
	private static final long serialVersionUID = 1L;
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }

	public DRGVersion getDRGVersion(String verCode) {
		return DRGVersion.get(DRGVersion.class, verCode);
	}
	
	public void creatDRGVersion(DRGVersion dRGVersion) {
		dRGVersion.save();
	}
	
	public void updateDRGVersion(DRGVersion dRGVersion) {
		dRGVersion .save();
	}
	
	public void removeDRGVersion(DRGVersion dRGVersion) {
		if(dRGVersion != null){
			dRGVersion.remove();
		}
	}
	
	public void removeDRGVersions(Set<DRGVersion> dRGVersions) {
		for (DRGVersion dRGVersion : dRGVersions) {
			dRGVersion.remove();
		}
	}
	
	public List<DRGVersion> findAllDRGVersion() {
		
		return DRGVersion.findAll(DRGVersion.class);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DRGVersionApplication#getDRGversion()
	 */
	@Override
	public String getDRGversion(String groupVersion) {
		String sql="select ver_code from DRGs.T_DRGVERSION t where ver_desc='"+groupVersion+"' order by t.add_date desc  ";
		String version = null;
		List<Object> resultList = getQueryChannelService().createSqlQuery(sql).list();
		if (resultList.size()>0) {
			version = resultList.get(0)+"";
		}
		return version;
	}
	@Override
	public String getDrgDescByVerCode(String code) {
		String sql="select t.ver_desc from DRGs.T_DRGVERSION t where t.ver_code ='"+code+"'   ";
		String desc = "";
		List<Object> resultList = getQueryChannelService().createSqlQuery(sql).list();
		if (resultList.size()>0) {
			desc = resultList.get(0)+"";
		}
		return desc;
	}
	@Override
	public String getDrgRegionByVerCode(String code) {
		String sql="select t.REGION_CODE from DRGs.T_DRGVERSION t where t.ver_code ='"+code+"'   ";
		String desc = "";
		List<Object> resultList = getQueryChannelService().createSqlQuery(sql).list();
		if (resultList.size()>0) {
			desc = resultList.get(0)+"";
		}
		return desc;
	}
	@Override
	public String getDrgVerDescByIcdType(String icdType) {
		String sql="select ver_desc from DRGs.T_DRGVERSION t where REGION_CODE='"+icdType+"' order by t.add_date desc  ";
		String ver_desc = null;
		List<Object> resultList = getQueryChannelService().createSqlQuery(sql).list();
		if (resultList.size()>0) {
			ver_desc = resultList.get(0)+"";
		}
		return ver_desc;
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DRGVersionApplication#getLatestDRGVersion(java.lang.String)
	 */
	@Override
	public String getLatestDRGVersion(String keyword) {
		String sql="SELECT d.VER_CODE FROM DRGS.T_DRGVERSION D WHERE D.VER_DESC LIKE '%"+ keyword +"%' ORDER BY d.VER_CODE DESC";
		String version = null;
		List<Object> resultList = getQueryChannelService().createSqlQuery(sql).list();
		if (resultList.size()>0) {
			version = resultList.get(0)+"";
		}
		return version;
	}

	@Override
	public List<DRGVersion> findAllDRGVersionOrder() {
		// TODO Auto-generated method stub
		List<DRGVersion> resultList = new ArrayList<DRGVersion>() ;
		String sql = "SELECT td.ver_code ,td.ver_desc from drgs.t_drgversion td order by td.add_date desc " ;
		List<Object[]> list = getQueryChannelService().createSqlQuery(sql).list() ;
		  for(Object[] objArr : list){
			  DRGVersion drgVer = new DRGVersion() ;
			  String verCode = (String) (objArr[0] != null ? objArr[0] : "") ;
			  String verDesc = (String) (objArr[1] != null ? objArr[1] : "") ;
			  drgVer.setVerCode(verCode);
			  drgVer.setVerDesc(verDesc);
			  resultList.add(drgVer) ;
		  }
		 return resultList ;
	}

	
	
}
